// import axios from '../node_modules/axios/dist/axios.js'
import axios from '@/js_sdk/gangdiedao-uni-axios'
// import axios from 'axios'
// import Qs from '../node_modules/@types/q/index.d.ts'
import Qs from 'qs'
// 感谢https://www.cnblogs.com/merryan-share/p/12418450.html
const http = axios.create({
	// baseURL: 'http://localhost:8888/onlineLearning',
	// baseURL: 'http://cn-cd-dx.sakurafrp.com:57580/onlineLearning',
	baseURL: 'http://cn-cd-dx.sakurafrp.com:57580/onlineLearning',
	withCredentials: true, // send cookies when cross-domain requests
	timeout: 20000, // request timeout
	crossDomain: true
})

//http request 拦截器
http.interceptors.request.use(config => {
	return config
}, err => {
	Message({
		message: '请求超时',
		type: 'error'
	})
	return Promise.reject(err)
})

// http response 拦截器
http.interceptors.response.use(response => {
	const data = response.data;
	if (data.success) { //如果返回success，statusCode==200时，不拦截
		return data
	}
	// 根据返回的code值来做不同的处理(和后端约定)
	switch (data.statusCode) {
		case '401':
			console.log('401:' + data.message)
			// return Promise.reject('401')
			return data
		case '600':
			// // 600时未登陆，导向登陆界面
			// Router.push({
			//     path: '/'
			// })
			// console.log('600:' + data.message)
			return Promise.reject('600')
		case '601':
			// // 601时已登录，导向主界面
			//   Router.push({
			//     path: '/about'
			//   })
			// console.log('601:' + data.message)
			return data
		default:
			// console.log('错误：' + data.message)
			// return Promise.reject('未知错误')
			return Promise.reject(data)
	}

}, (err) => { //响应失败时的处理
	return Promise.reject(err)
})

//封装 getRequest
export function getRequest(url, params) {
	return http({
		method: 'get',
		url: `${url}`,
		params: params
	})
}

// 传入的参数
export function postRequest(url, params) {

	return http({
		method: 'post',
		url: `${url}`,
		data: params,
		transformRequest: [function(data) {

			return Qs.stringify(data, {
				allowDots: true
			})
		}],
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded'
		}
	})
}

// 传入的参数为 JSON
// 后端需要 @RequestBody
//封装 postRequest-json数据格式
export function postJsonRequest(url, params) {
	return http({
		method: 'post',
		url: `${url}`,
		data: params,
		headers: {
			'Content-Type': 'application/json'
		}
	})
}
